<?php 
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\InformationPart;
use \PsiFramework\Applications\AlloMediaUser\Classes\Actions\ShopAction;

$user = $response->getAttribute("user");
$order = $response->getAttribute('invoiceOrder');
$currency = $order->getCurrency();
$currencySymbol = $currency->getSymbol();

$recalculate=false;
$title = $languageManager->getText('INVOICE');
if (!$order->ispaid()) {
	$title = $languageManager->getText('ORDER');
	$recalculate=true;
}

$orderArray = ShopAction::getOrder($order, $recalculate);
$customerEmail = $user->getEmail();

$customerName = $orderArray['customerName'];
$customerAddress = $orderArray['customerAddress'];
$customerZipCode = $orderArray['customerZipCode'];
$customerCity = $orderArray['customerCity'];

$password = $response->getAttribute('paymentPassword');
?>
<article id="cont-princ" class="nocols">
    <?php $templateManager->displayTemplate('Steps');  ?>
	<!-- FACTURE -->
	<div id="facturation">
		<ul class="toolbar donotprint">
			<li>
			    <a
			        href="javascript:window.open('<?php echo HTML::linkHref('action=PrintShowInvoice&order_id='.$order->getId()); ?>', 'Facture', 'menubar=no, status=no, width=700, height=900');"
			        title="<?php Printer::display($languageManager->getText('PRINTABLE_VERSION')); ?>">
			        <img src="Images/AlloMedia/ico/ico_print.png" />
		        </a>
		    </li>
			<li>
			    <a
			        href="<?php echo HTML::linkHref('action=PdfShowInvoice&order_id='.$order->getId()); ?>"
			        title="<?php Printer::display($languageManager->getText('DOWNLOAD_PDF')); ?>">
			        <img src="Images/AlloMedia/ico/ico_pdf.png" />
		        </a>
		    </li>
		</ul>
		<table class="tableHaut">
			<tbody>
				<tr>
					<td>
						<img src="Images/AlloMedia/logo_allomedia.png" alt="Allo-Media Groupe" />
					</td>
					<td style="width:50%;">
						<table class="factNom">
							<tbody>
								<tr>
									<td><?php Printer::display($title); ?> n°<?php Printer::display($orderArray['number']); ?></td>
								</tr>
								<tr>
									<td class="factMarge"></td>
								</tr>
							</tbody>
						</table>
						<table class="factDetails">
							<tbody>
								<tr>
									<td><b><?php Printer::display($languageManager->getText('ORDER')); ?> :</b></td>
									<td><?php Printer::display($orderArray['number']); ?></td>
								</tr>
								<tr>
									<td><b><?php Printer::display($languageManager->getText('DATE')); ?> :</b></td>
									<td><?php Printer::display($orderArray['date']); ?></td>
								</tr>
								<tr>
									<td><b><?php Printer::display($languageManager->getText('PAYMENT')); ?> :</b></td>
									<td><?php Printer::display($languageManager->getText($orderArray['mean'])); ?></td>
								</tr>
							</tbody>
						</table>
					</td>
				</tr>
				<tr>
					<td class="factMarge"></td>
				</tr>
				<tr>
					<td>
						<table class="factExp">
							<tbody>
								<tr>
									<td>
										<span style="color:#257eac;"><b>Groupe Allo-Media</b></span><br />
										3 villa du Poitou<br />
										95150 Taverny
									</td>
								</tr>
								<tr>
									<td>SIRET : 53116321000010</td>
								</tr>
							</tbody>
						</table>
					</td>
					<td>
						<table class="factAdresse">
							<thead>
								<th><?php Printer::display($languageManager->getText('INVOICE_ADDRESS')); ?></th>
							</thead>
							<tbody>
								<tr>
									<td>
									    <b>
									        <?php 
									        Printer::display($customerName);
									        ?>
								        </b>
								    </td>
								</tr>
								<tr>
									<td>
									    <?php
									    Printer::display($customerAddress);
									    ?>
								    </td>
								</tr>
								<tr>
									<td>
									    <?php
									    Printer::display($customerZipCode.' '.$customerCity);
								        ?>
								    </td>
								</tr>
							</tbody>
						</table>
					</td>
				</tr>
			</tbody>
		</table>
		<div class="factTitre"><?php Printer::display($title); ?></div>
		<table class="tableCorps">
			<thead class="tableTete">
				<tr>
					<th class="col0 tableTitre">#</th>
					<th class="col1 tableTitre"><?php Printer::display($languageManager->getText('PRODUCT')); ?></th>
					<th class="col3 tableTitre"><?php Printer::display($languageManager->getText('SUBSCRIPTION_LENGTH')); ?></th>
					<th class="col4 tableTitre"><?php Printer::display($languageManager->getText('PRICE')); ?></th>
				</tr>
			</thead>
            <tbody>
                <?php
                $total = 0;
                $realTotal = 0;
                
                $i = 1 ;
                foreach ($orderArray['items'] as $itemArray) {
                ?>
                <tr>
                    <td class="col0 tableTexte"><?php Printer::display($i); ?></td>
                    <td class="col1 tableTexte">
                        <?php
                        Printer::display($itemArray['itemName']);
                        ?>
                        <br/>
                        <sub>
                        <?php 
                        Printer::display($itemArray['itemSubtext']);
                        ?>
                        </sub>
                    </td>
                    <td class="col3 tableTexte">
                    <?php 
                        Printer::display($itemArray['quantityLabel']);
                    ?>
                    </td>
                    <td class="col4 tableTexte">
                        <?php Printer::display(number_format($itemArray['realSubTotal'], 2, '.', ' ').$currencySymbol); ?>
                    </td>
                </tr>
                <?php
                    $i++;
                }
                ?>
            </tbody>
		</table>
		<table class="tablePied">
			<tbody>
				<tr>
					<td class="factMarge"></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td style="width:50%;">
						<table class="factTotal">
							<tbody>
							    <tr>
                					<td><b><?php Printer::display($languageManager->getText('PRICE_WO_TAXES')); ?></b></td>
                					<td class="prix">
                					    <?php Printer::display(number_format($orderArray['realTotal'], 2, '.', ' ').$currencySymbol); ?>
                				    </td>
                				</tr>
                				<?php 
                				foreach ($orderArray['taxes'] as $tax) {
                				?>
                				<tr>
									<td>
									    <b>
									        <?php Printer::display($tax['tax']->getName().' ('.number_format($tax['value'], 2, '.', '').'%)'); ?>
								        </b>
								    </td>
									<td class="prix">
                					    <?php Printer::display(number_format($tax['total'], 2, '.', ' ').$currencySymbol); ?>
                				    </td>
								</tr>
								<?php 
                				}
								?>
                				<tr>
                					<td class="total"><b><?php Printer::display($languageManager->getText('PRICE_W_TAXES')); ?></b></td>
                					<td class="prix">
                					    <?php 
                					        Printer::display(number_format($orderArray['paymentTotal'], 2, '.', ' ').$currencySymbol);
            					        ?>
                				    </td>
                				</tr>
							</tbody>
						</table>
					</td>
				</tr>
			</tbody>
		</table>
		<table class="factMentions">
			<tbody>
				<tr>
					<td>
						<b>Groupe Allo-Media - 3 villa du Poitou - 95150 Taverny</b><br />
						SIRET : 53116321000010
					</td>
				</tr>
			</tbody>
		</table>
		<ul class="toolbar donotprint">
			<li>
			    <a
			        href="javascript:window.open('<?php echo HTML::linkHref('action=PrintShowInvoice&order_id='.$order->getId()); ?>', 'Facture', 'menubar=no, status=no, width=700, height=900');"
			        title="<?php Printer::display($languageManager->getText('PRINTABLE_VERSION')); ?>">
			        <img src="Images/AlloMedia/ico/ico_print.png" />
		        </a>
		    </li>
			<li>
			    <a
			        href="<?php echo HTML::linkHref('action=PdfShowInvoice&order_id='.$order->getId()); ?>"
			        title="<?php Printer::display($languageManager->getText('DOWNLOAD_PDF')); ?>">
			        <img src="Images/AlloMedia/ico/ico_pdf.png" />
		        </a>
		    </li>
		</ul>
	</div>
</article>